package tron.common;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@RestController
public class BaseController {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public String getToken() {
        String token;
        try {
            HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
            token = request.getHeader("Token");
        } catch (Exception e) {
            throw new RuntimeException("用户Token失效");
        }
        return token;
    }

    // todo 根据token 获取当前用户信息

    public JSONObject getCache(String cacheName,String cacheId) {
        final String test = stringRedisTemplate.boundValueOps(cacheName + "::"+ cacheId).get();
        return JSONObject.parseObject(test);
    }
}
